#!/usr/bin/env ruby

desc 'Get code coverage report and run complexity analysis for Sonar'
lane :sonar_upload do
  next unless is_check_required(sources: sources_matrix[:llc], force_check: @force_check)

  version_number = get_version_number(
    xcodeproj: 'StreamChat.xcodeproj',
    target: 'StreamChat'
  )[/\d+\.\d+\.\d/]

  Dir.chdir('..') do
    sh("./fastlane/sonar/bin/sonar-scanner " \
       "-Dsonar.projectVersion=#{version_number} " \
       "-Dproject.settings=sonar-project.properties " \
       "-Dsonar.coverageReportPaths='reports/sonarqube-generic-coverage.xml'")
  end

  next if ENV['GITHUB_EVENT_NAME'] == 'pull_request' || current_branch !~ /main|develop/

  slack_sonarcloud_metrics(version_number: version_number)
end

desc 'Creates a report in Slack with SonarCloud analysis details'
private_lane :slack_sonarcloud_metrics do |options|
  project_key = 'GetStream_stream-chat-swift'
  metrics = sonarcloud_metric_kit(
    project_key: project_key,
    quality_gate: true,
    branch: current_branch
  )
  new_period = current_branch == 'main' ? options[:version_number] : current_branch
  new_coverage = metrics[:new_coverage].to_f.round(2)
  new_duplication_density = metrics[:new_duplicated_lines_density].to_f.round(2)
  vulnerabilities = metrics[:security_hotspots].to_i + metrics[:vulnerabilities].to_i
  new_vulnerabilities = metrics[:new_security_hotspots].to_i + metrics[:new_vulnerabilities].to_i

  release_status = "From *#{metrics[:period_value]}* to *#{new_period}*\n _#{metrics[:new_lines_to_cover]}_ new lines of code 🚀"
  coverage_status = "Total: _#{metrics[:coverage]}%_\nOn new code: _#{new_coverage}%_ #{new_coverage < 80 ? '⚠️' : '✅'}"
  duplication_status = "Total: _#{metrics[:duplicated_lines_density]}%_\nOn new code: _#{new_duplication_density}%_ #{new_duplication_density < 3 ? '✅' : '⚠️'}"
  code_smells_status = "Total: _#{metrics[:code_smells]}_\nNew: _#{metrics[:new_code_smells]}_ #{metrics[:new_code_smells].to_i.positive? ? '🙊' : '✅'}"
  bugs_status = "Total: _#{metrics[:bugs]}_\nNew: _#{metrics[:new_bugs]}_ #{metrics[:new_bugs].to_i.positive? ? '⚠️' : '✅'}"
  security_status = "Total: _#{vulnerabilities}_\nNew: _#{new_vulnerabilities}_ #{new_vulnerabilities.positive? ? '⚠️' : '✅'}"

  slack(
    message: "[StreamChat](https://sonarcloud.io/summary/new_code?id=#{project_key}&branch=#{current_branch})",
    attachment_properties: {
      thumb_url: 'https://pbs.twimg.com/profile_images/955394530806829056/LC7DAYM3_400x400.jpg',
      fields: [
        { title: 'Update', value: release_status, short: true },
        { title: 'Code coverage', value: coverage_status, short: true },
        { title: 'Duplicated lines density', value: duplication_status, short: true },
        { title: 'Code smells', value: code_smells_status, short: true },
        { title: 'Bugs', value: bugs_status, short: true },
        { title: 'Vulnerabilities & Security hotspots', value: security_status, short: true }
      ]
    },
    default_payloads: [],
    slack_url: ENV.fetch('SLACK_WEBHOOK_URL', nil)
  )
end
